/**
 * Copyright (C)
 * FileName: ItemController
 * Author:   86151
 * Date:     2019/7/19 11:57
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hand.llq25404.controller;

import com.hand.llq25404.entity.SsmItem;
import com.hand.llq25404.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ItemController {

    @Autowired
    private IItemService iItemService;

    @RequestMapping("/index")
    public String index(){
        return "forward:/pagingquery";
    }

    @RequestMapping("/pagingquery")
    public ModelAndView pagingQuery(ModelAndView mv,String page){

//        PageHelper.startPage(1,5);
//        List<SsmItem> list = iItemService.getSsmItems();
//        PageInfo page = new PageInfo(list);
//        long count = page.getTotal();
       // 第一次访问默认显示第一页数据
        int pageNum = 1;
        // 页码
        String pn = page;
        if (pn != null) {
            pageNum = Integer.parseInt(pn);
        }
        // 每页显示条数
        int pageSize = 5;
        // 从那一条开始查
        int start = (pageNum - 1) * pageSize;
        List<SsmItem> list = iItemService.pagingQuery(start,pageSize);
        mv.addObject("count", iItemService.getSsmItems().size());
        mv.addObject("pageNum", pageNum);
        //物料信息
        mv.addObject("list",list);
        mv.setViewName("index");
        return mv;
    }

    /**
     * 查询
     * @param mv
     * @param page
     * @return
     */
    @RequestMapping("/fuzzyQuery")
    public ModelAndView fuzzyQuery(ModelAndView mv,String itemCode,String itemDescription,String itemUom,
      String startActiveDate, String endActiveDate,String enabledFlag,String page){
        System.out.println(itemCode);
        //第一次访问默认显示第一页数据
        int pageNum = 1;
        // 页码
        String pn = page;
        if (pn != null) {
            pageNum = Integer.parseInt(pn);
        }
        // 每页显示条数
        int pageSize = 5;
        // 从那一条开始查
        int start = (pageNum - 1) * pageSize;
        List<SsmItem> list = iItemService.fuzzyQuery(start,pageSize,itemCode,itemDescription,itemUom,
                startActiveDate,endActiveDate,enabledFlag);
        mv.addObject("count", iItemService.getCount(itemCode,itemDescription,itemUom,
                startActiveDate,endActiveDate,enabledFlag).size());
        mv.addObject("pageNum", pageNum);
        System.out.println(list);
        //物料信息
        mv.addObject("list",list);
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/addItem")
    public String addItem(SsmItem ssmItem){
        System.out.println(ssmItem);
        iItemService.addItem(ssmItem);
        return "forward:/pagingquery";
    }

    @RequestMapping("/delItem")
    public String delItem(String itemId){
        iItemService.delItem(itemId);
        return "forward:/pagingquery";
    }

    @RequestMapping("/queryId")
    @ResponseBody
    public SsmItem queryId(Long itemId){
        SsmItem ssmItem = iItemService.queryId(itemId);
        return ssmItem;
    }

    @RequestMapping("/update")
    public String update( SsmItem ssmItem,Long itemId){
        System.out.println(ssmItem);
        System.out.println(itemId);
        iItemService.update(ssmItem,itemId);
        return "forward:/pagingquery";
    }

    @RequestMapping("/batch")
    public String batch(String ids){
        String[] id = ids.split(",");
        for (int i  = 0 ; i < id.length ; i ++) {
            iItemService.delItem(id[i]);
            System.out.println(id[i]);
        }
        return "forward:/pagingquery";
    }

}
